package com.yly.doctorapp.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yly.doctorapp.R;
import com.yly.doctorapp.base.BaseActivity;
import com.yly.doctorapp.base.DoctorAppApplication;
import com.yly.doctorapp.bean.ResponsDataBean;
import com.yly.doctorapp.bean.UserBean;
import com.yly.doctorapp.constant.Configution;
import com.yly.doctorapp.ui.home.HomePagerActivity;
import com.zhcx.zhcxlibrary.utils.SPUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.v_phone)
    View vPhone;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.iv_show)
    ImageView ivShow;
    @BindView(R.id.v_password)
    View vPassword;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_forgetpassword)
    TextView tvForgetpassword;
    private SPUtils mSputils;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSputils = new SPUtils(getApplicationContext());
        editPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    vPhone.setBackgroundColor(0xffa0524b);
                    vPassword.setBackgroundColor(0xffcccccc);
                }
            }
        });
        editPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    vPhone.setBackgroundColor(0xffcccccc);
                    vPassword.setBackgroundColor(0xffa0524b);
                }
            }
        });
    }

    @Override
    protected void initData() {
        if (StringUtils.isEmpty(mSputils.getString(Configution.USERPHONE))){
            editPhone.setText("");
        }else {
            editPhone.setText(mSputils.getString(Configution.USERPHONE));
        }
        if (StringUtils.isEmpty(mSputils.getString(Configution.USERPASSWORD))){
            editPassword.setText("");
        }else {
            editPassword.setText(mSputils.getString(Configution.USERPASSWORD));
        }
    }

    private int count = 0;

    @OnClick({R.id.iv_show, R.id.tv_login, R.id.tv_forgetpassword})
    public void onViewClicked(View view) {
        Intent mIntent = null;
        switch (view.getId()) {
            case R.id.iv_show:
                if ((count % 2) == 0) {
                    ivShow.setImageResource(R.mipmap.passwordshow);
                    editPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ivShow.setImageResource(R.mipmap.passwordnoshow);
                    editPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                count++;
                break;
            case R.id.tv_login:
                if (StringUtils.isEmpty(editPhone.getText().toString())) {
                    ToastUtils.show(getApplicationContext(), "请输入账号", 100);
                    return;
                }
                if (editPhone.getText().toString().length() != 11) {
                    ToastUtils.show(getApplicationContext(), "请输入正确账号", 100);
                    return;
                }
                if (!StringUtils.isPhoneNumber(editPhone.getText().toString())){
                    ToastUtils.show(getApplicationContext(), "请输入正确账号", 100);
                    return;
                }
                if (StringUtils.isEmpty(editPassword.getText().toString())) {
                    ToastUtils.show(getApplicationContext(), "请输入密码", 100);
                    return;
                }
                initLogin(editPhone.getText().toString(), editPassword.getText().toString());
                break;
            case R.id.tv_forgetpassword:
                mIntent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                mIntent.putExtra("phone", editPhone.getText().toString());
                mIntent.putExtra("type", "0");//
                startActivity(mIntent);
                break;
        }
    }

    private void initLogin(final String phone, final String password) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.LOGIN);
        params.addBodyParameter("telephone", phone);
        params.addBodyParameter("pwd", password);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)){
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")){
                        if (!StringUtils.isEmpty(responsDataBean.getData())){
                            mSputils.putString(Configution.USERPHONE, phone);
                            mSputils.putString(Configution.USERPASSWORD, password);
                            DoctorAppApplication.mUserBean = JSON.parseObject(responsDataBean.getData(), UserBean.class);
                            Intent mIntent = new Intent(LoginActivity.this, HomePagerActivity.class);
                            mIntent.putExtra("type", "1");
                            startActivity(mIntent);
                            finish();
                        }
                    }else {
                        ToastUtils.show(getApplicationContext(), responsDataBean.getMsg(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接中断", 100);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
